import 'package:environmental_les_app/common/net/net.dart';
import 'package:environmental_les_app/common/utils.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';
import 'package:flutter_clx_base/widget/base_list_widget.dart';

class EventListController extends GetxController
    with GetTickerProviderStateMixin {
  EventListController();

  final listController = BaseListController();

  List<String> tabs = ["事件巡查", "设备巡查"];

  int tabClickIndex = 0; // tab 点击index

  //当前时间筛选结果
  Map<String, dynamic> allTime = {};

  // 当前选中的筛选项，prop -> list of selected values
  final Map<String, List<String>> selectedFilters = {};
  late TabController tabController;

  // 筛选项
  final List<Map<String, dynamic>> filterData = [
    {
      "title": "业务类型",
      "prop": "siteType",
      "multiSelect": false,
      "filter": [
        {"name": "巡查事件", "value": "1"},
        {"name": "报警线索", "value": "2"},
        {"name": "待办任务", "value": "3"},
      ],
    },
    {
      "title": "巡查状态",
      "prop": "alarmType",
      "multiSelect": false,
      "filter": [
        {"name": "未巡查", "value": "1"},
        {"name": "已巡查", "value": "2"},
      ],
    },
    {
      "title": "是否督办",
      "prop": "status",
      "multiSelect": true,
      "filter": [
        {"name": "是", "value": "1"},
        {"name": "否", "value": "2"},
      ],
    },
  ];

  // 更新选中结果（来自弹窗返回）
  void updateSelection(Map<String, dynamic>? result) {
    updateSelectedFilters(selectedFilters, filterData, result);
    update(["warning_list"]);
  }

  /// tab 点击
  onTabClick(index) {
    tabClickIndex = index;
    _initData();
  }

  _initData() {
    update(["event_list"]);
  }

  void onTap() {}

  void getList(page, pageSize, success, error) {
    DioUtils.instance.getMockAdapter().onGet(
          ServiceApi.eventList,
          (server) => server.reply(
              200,
              {
                "data": {
                  "records": List.generate(5, (index) {
                    return {
                      "title": "标题 $index",
                      "subtitle": "这是第 $index 条数据",
                    };
                  }),
                },
                "msg": "success",
                "code": 0
              },
              delay: const Duration(seconds: 1)),
        );

    NetUtils.get(ServiceApi.eventList, queryParameters: null,
        successCallback: (data) {
      success(data['records']);
    }, errorCallback: error);
  }

  @override
  void onInit() {
    super.onInit();
    tabController = TabController(length: tabs.length, vsync: this);
  }

  @override
  void onReady() {
    super.onReady();
    _initData();
  }

  // @override
  // void onClose() {
  //   super.onClose();
  // }
}
